package com.hb.trade.api.routine.controller.project.evaluation;

import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.project.evaluation.dto.*;
import com.hb.core.pojo.project.evaluation.vo.*;
import com.hb.core.pojo.project.file.dto.ReviewEvaluationFormDTO;
import com.hb.core.pojo.project.file.dto.ReviewEvaluationGenerateFormDTO;
import com.hb.trade.api.routine.util.ReviewItemExcelUtil;
import com.hb.trade.client.project.EvaluationClient;
import com.hb.trade.client.project.ReviewItemClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 评标打分模块接口
 *
 * @author jinhaolin
 * @date 2022/4/14 0014 10:10
 **/
@Slf4j
@Version(1.0)
@Api(tags = {"评标打分相关"})
@RequestMapping("/{version}")
@RestController
public class ApiEvaluationController {

    protected final EvaluationClient evaluationClient;
    protected final ReviewItemClient reviewItemClient;

    public ApiEvaluationController(EvaluationClient evaluationClient,
                                   ReviewItemClient reviewItemClient) {
        this.evaluationClient = evaluationClient;
        this.reviewItemClient = reviewItemClient;
    }

    @ApiOperation(value = "获取评审项下的所有评审结果信息", httpMethod = "POST")
    @PostMapping("/evaluation/allResultOfReviewPoint")
    public ResponseDto<EvaluationReviewItemResultVO> listNotSelectSection(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        Long id = ShiroUtil.getUserId();
        String userName = ShiroUtil.getUser().getFullName();
        evaluationReviewItemParam.setExpertId(id);
        evaluationReviewItemParam.setExpertName(userName);

        Message<EvaluationReviewItemResultVO> result = evaluationClient.allResultOfReviewPoint(evaluationReviewItemParam);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "批量通过", httpMethod = "POST")
    @PostMapping("/evaluation/batchPass")
    ResponseDto<Boolean> batchPass(@RequestBody List<EvaluationSubmitScoreParam> evaluationSubmitScoreParamList) {
        Message<Boolean> result = evaluationClient.batchPass(evaluationSubmitScoreParamList);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "批量拒绝", httpMethod = "POST")
    @PostMapping("/evaluation/batchRefuse")
    public ResponseDto<Boolean> batchRefuse(@RequestBody List<EvaluationSubmitScoreParam> evaluationSubmitScoreParamList) {
        Message<Boolean> result = evaluationClient.batchRefuse(evaluationSubmitScoreParamList);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "保存单个企业的单个评审点的评标结果", httpMethod = "POST")
    @PostMapping("/evaluation/saveScoreOfReviewPointSingleOrg")
    public ResponseDto<Boolean> saveScoreOfReviewPointSingleOrg(@RequestBody EvaluationSubmitScoreParam evaluationSubmitScoreParam) {
        Message<Boolean> result = evaluationClient.saveScoreOfReviewPointSingleOrg(evaluationSubmitScoreParam);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "保存批量评标结果", httpMethod = "POST")
    @PostMapping("/evaluation/saveScoreBatch")
    public ResponseDto<Boolean> saveScoreBatch(@RequestBody List<EvaluationSubmitScoreParam> evaluationSubmitScoreParamList) {
        Message<Boolean> result = evaluationClient.saveScoreBatch(evaluationSubmitScoreParamList);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "提交单个评审点的评标结果", httpMethod = "POST")
    @PostMapping("/evaluation/submitScoreOfReviewPoint")
    public ResponseDto<Boolean> submitScoreOfReviewPoint(@RequestBody List<EvaluationSubmitScoreParam> evaluationSubmitScoreParamList) {
        Message<Boolean> result = evaluationClient.submitScoreOfReviewPoint(evaluationSubmitScoreParamList);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "提交单个评审点的评标结果（组长对报价评审）", httpMethod = "POST")
    @PostMapping("/evaluation/submitScoreOfReviewPointByOffer")
    public ResponseDto<Boolean> submitScoreOfReviewPointByOffer(@RequestBody List<EvaluationSubmitScoreParam> evaluationSubmitScoreParamList) {
        Message<Boolean> result = evaluationClient.submitScoreOfReviewPointByOffer(evaluationSubmitScoreParamList);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "提交并共享单个评审点的评标结果", httpMethod = "POST")
    @PostMapping("/evaluation/submitAndShareScoreOfReviewPoint")
    public ResponseDto<Boolean> submitAndShareScoreOfReviewPoint(@RequestBody List<EvaluationSubmitScoreParam> evaluationSubmitScoreParamList) {
        Message<Boolean> result = evaluationClient.submitAndShareScoreOfReviewPoint(evaluationSubmitScoreParamList);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "提交评审项下所有评审点的评标结果", httpMethod = "POST")
    @PostMapping("/evaluation/submitScoreOfReviewItem")
    public ResponseDto<Boolean> submitScoreOfReviewItem(@RequestBody List<EvaluationSubmitScoreParam> evaluationSubmitScoreParamList) {
        Message<Boolean> result = evaluationClient.submitScoreOfReviewItem(evaluationSubmitScoreParamList);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "获取项目及标段信息", httpMethod = "POST")
    @PostMapping("/evaluation/bidProjectAndSectionInfo")
    public ResponseDto<EvaluationProjectAndSectionInfoVO> bidProjectAndSectionInfo(@RequestBody EvaluationBidSectionParam evaluationBidSectionParam) {
        Message<EvaluationProjectAndSectionInfoVO> evaluationProjectAndSectionInfoVOMessage = evaluationClient.bidProjectAndSectionInfo(evaluationBidSectionParam);
        return ResultUtil.msg(evaluationProjectAndSectionInfoVOMessage);
    }

    @ApiOperation(value = "获取评标流程节点", httpMethod = "POST")
    @PostMapping("/evaluation/reviewItemFlowNodes")
    public ResponseDto<EvaluationReviewItemFlowNodesVO> reviewItemFlowNodes(@RequestBody EvaluationBidSectionParam evaluationBidSectionParam) {
        Long id = ShiroUtil.getUserId();
        evaluationBidSectionParam.setExpertId(id);
        Message<EvaluationReviewItemFlowNodesVO> evaluationReviewItemFlowNodesVOMessage = evaluationClient.reviewItemFlowNodes(evaluationBidSectionParam);
        return ResultUtil.msg(evaluationReviewItemFlowNodesVOMessage);
    }

    @ApiOperation(value = "获取线下评标流程节点", httpMethod = "POST")
    @PostMapping("/evaluation/reviewItemFlowNodesOffline")
    public ResponseDto<EvaluationReviewItemFlowNodesVO> reviewItemFlowNodesOffline(@RequestBody EvaluationBidSectionParam evaluationBidSectionParam) {
        Long id = ShiroUtil.getUserId();
        evaluationBidSectionParam.setExpertId(id);
        Message<EvaluationReviewItemFlowNodesVO> evaluationReviewItemFlowNodesVOMessage = evaluationClient.reviewItemFlowNodesOffline(evaluationBidSectionParam);
        return ResultUtil.msg(evaluationReviewItemFlowNodesVOMessage);
    }

    @ApiOperation(value = "获取可复用评审信息", httpMethod = "POST")
    @PostMapping("/evaluation/reusableScoreOfReviewPoint")
    public ResponseDto<List<EvaluationReusableScoreItemVO>> reusableScoreOfReviewPoint(@RequestBody EvaluationReviewPointParam evaluationReviewPointParam) {
        Message<List<EvaluationReusableScoreItemVO>> result = evaluationClient.reusableScoreOfReviewPoint(evaluationReviewPointParam);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "获取汇总结果", httpMethod = "POST")
    @PostMapping("/evaluation/resultSummaryInfo")
    public ResponseDto<EvaluationResultSummaryVO> resultSummaryInfo(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        Long id = ShiroUtil.getUserId();
        String userName = ShiroUtil.getUser().getFullName();
        evaluationReviewItemParam.setExpertId(id);
        evaluationReviewItemParam.setExpertName(userName);

        Message<EvaluationResultSummaryVO> result = evaluationClient.resultSummaryInfo(evaluationReviewItemParam);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "确认汇总", httpMethod = "POST")
    @PostMapping("/evaluation/confirmResultSummary")
    public ResponseDto<Boolean> confirmResultSummary(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        Message<Boolean> booleanMessage = evaluationClient.confirmResultSummary(evaluationReviewItemParam);
        return ResultUtil.msg(booleanMessage);
    }

    @ApiOperation(value = "复核", httpMethod = "POST")
    @PostMapping("/evaluation/recheckResultSummary")
    public ResponseDto<Boolean> recheckResultSummary(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        Long id = ShiroUtil.getUserId();
        String userName = ShiroUtil.getUser().getFullName();
        evaluationReviewItemParam.setExpertId(id);
        evaluationReviewItemParam.setExpertName(userName);

        Message<Boolean> booleanMessage = evaluationClient.recheckResultSummary(evaluationReviewItemParam);
        return ResultUtil.msg(booleanMessage);
    }


    /**
     * 查询报价评审信息
     * @author tian
     * @date 2022-4-20 15:21
     * @param evaluationReviewItemParam
     * @return org.fzzn.core.model.msg.ResponseDto<java.util.Map>
     */
    @ApiOperation(value = "查询报价评审信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewItemId", value = "二级评审点id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class)
    })
    @PostMapping("/evaluation/findQuotationReview")
    public ResponseDto<Map> findQuotationReview(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        if(null == evaluationReviewItemParam.getReviewItemId() || null == evaluationReviewItemParam.getSectionId()){
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        evaluationReviewItemParam.setExpertId(ShiroUtil.getUserId());
        Message<Map> message = evaluationClient.findQuotationReview(evaluationReviewItemParam);
        return ResultUtil.msg(message);
    }


    /**
     * 非组长确认报价
     * @author tian
     * @date 2022-4-20 16:04
     * @param evaluationReviewItemParam
     * @return org.fzzn.core.model.msg.ResponseDto<java.util.Map>
     */
    @ApiOperation(value = "非组长确认报价", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewItemId", value = "二级评审点id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class)
    })
    @PostMapping("/evaluation/confirmOffer")
    public ResponseDto confirmOffer(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        if(null == evaluationReviewItemParam.getReviewItemId() || null == evaluationReviewItemParam.getSectionId()){
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        // 标记当前登录的专家
        evaluationReviewItemParam.setExpertId(ShiroUtil.getUserId());
        Message<String> message = evaluationClient.confirmOffer(evaluationReviewItemParam);
        return ResultUtil.msg(message);
    }



    /**
     * 查询专家报价信息提交状态
     * @author tian
     * @date 2022-4-20 17:39
     * @param evaluationReviewItemParam
     * @return org.fzzn.core.model.msg.ResponseDto<java.lang.String>
     */
    @ApiOperation(value = "查询专家报价信息提交状态", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewItemId", value = "二级评审点id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class)
    })
    @PostMapping("/evaluation/selectOfferReview")
    public ResponseDto selectOfferReview(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        if(null == evaluationReviewItemParam.getReviewItemId() || null == evaluationReviewItemParam.getSectionId()){
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        Message<List<EvaluationSecondDTO>> message = evaluationClient.selectOfferReview(evaluationReviewItemParam);
        return ResultUtil.msg(message);
    }


    /**
     * 结束评标
     * @author tian
     * @date 2022-4-20 17:39
     * @param evaluationReviewItemParam
     * @return org.fzzn.core.model.msg.ResponseDto<java.lang.String>
     */
    @ApiOperation(value = "结束评标", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class)
    })
    @PostMapping("/evaluation/endReview")
    public ResponseDto endReview(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        if(null == evaluationReviewItemParam.getSectionId()){
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        Message<String> message = evaluationClient.endReview(evaluationReviewItemParam);
        return ResultUtil.msg(message);
    }

    /**
     *
     * @Description:评标专家系统生成汇总表格
     * @Author: zhongdl
     * @Date:2022/4/22 9:52
     * @param evaluationSummaryAddDTO:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/evaluation/generateEvaluationSummary")
    public ResponseDto<Boolean> generateEvaluationSummary(@RequestBody EvaluationSummaryAddDTO evaluationSummaryAddDTO){
        Message<Boolean> booleanMessage = evaluationClient.generateEvaluationSummary(evaluationSummaryAddDTO);
        return ResultUtil.msg(booleanMessage);
    }
    /**
     *
     * @Description:评标专家系统生成汇总表格更新状态
     * @Author: zhongdl
     * @Date:2022/4/22 9:52
     * @param evaluationSummaryAddDTO:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/evaluation/updateEvaluationSummaryStatus")
    public ResponseDto<Boolean> updateEvaluationSummaryStatus(@RequestBody EvaluationSummaryAddDTO evaluationSummaryAddDTO){
        Message<Boolean> booleanMessage = evaluationClient.updateEvaluationSummaryStatus(evaluationSummaryAddDTO);
        return ResultUtil.msg(booleanMessage);
    }

    /**
     *
     * @Description:评标专家系统生成汇总表格
     * @Author: zhongdl
     * @Date:2022/4/22 9:52
     * @param evaluationSummaryAddDTO:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/evaluation/evaluationSummaryDetails")
    public ResponseDto evaluationSummaryDetails(@RequestBody EvaluationSummaryAddDTO evaluationSummaryAddDTO){
        log.info("evaluationSummaryDetails接口参数:{}",evaluationSummaryAddDTO.getSectionId());
        Message<EvaluationSummaryDTO> summaryDTOMessage = evaluationClient.evaluationSummaryDetails(evaluationSummaryAddDTO);
        if (summaryDTOMessage.getData() == null || summaryDTOMessage.getData().getId() == null){
            evaluationClient.generateEvaluationSummary(evaluationSummaryAddDTO);
        }
        summaryDTOMessage = evaluationClient.evaluationSummaryDetails(evaluationSummaryAddDTO);
        EvaluationSummaryDTO data = summaryDTOMessage.getData();
        if (StrUtil.isNotEmpty(data.getSheetUrl())){
            return ResultUtil.msg(summaryDTOMessage);
        }
        //合成表格
        ReviewEvaluationGenerateFormDTO reviewEvaluationGenerateFormDTO = new ReviewEvaluationGenerateFormDTO();
        reviewEvaluationGenerateFormDTO.setSectionId(evaluationSummaryAddDTO.getSectionId());
        Message<List<ReviewEvaluationFormDTO>> reviewEvaluationGenerateFormDTOMessage = reviewItemClient.generate(reviewEvaluationGenerateFormDTO);
        List<ReviewEvaluationFormDTO> reviewEvaluationForm = reviewEvaluationGenerateFormDTOMessage.getData();

        String url = ReviewItemExcelUtil.mergExcel(reviewEvaluationForm,evaluationSummaryAddDTO.getSectionId());
        if (StrUtil.isNotEmpty(url)){
            EvaluationSummaryAddDTO evaluationSummary = new EvaluationSummaryAddDTO();
            evaluationSummary.setSectionId(evaluationSummaryAddDTO.getSectionId());
            evaluationSummary.setSheetUrl(url);
            evaluationClient.updateEvaluationSummaryStatus(evaluationSummary);
        }
        summaryDTOMessage = evaluationClient.evaluationSummaryDetails(evaluationSummaryAddDTO);
        return ResultUtil.msg(summaryDTOMessage);
    }

    /**
     * 查询最终汇总详情
     * @author tian
     * @date 2022-4-20 17:39
     * @param evaluationReviewItemParam
     * @return org.fzzn.core.model.msg.ResponseDto
     */
    @ApiOperation(value = "查询最终汇总详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class)
    })
    @PostMapping("/evaluation/finalSummaryReview")
    public ResponseDto finalSummaryReview(@RequestBody EvaluationReviewItemParam evaluationReviewItemParam) {
        if(null == evaluationReviewItemParam.getSectionId()){
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        Message<Map> message = evaluationClient.finalSummaryReview(evaluationReviewItemParam);
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "项目信息中的开标一览表", httpMethod = "POST")
    @PostMapping("/evaluation/openingSchedule")
    public ResponseDto<EvaluationOpeningScheduleVO> openingSchedule(@RequestBody EvaluationBidSectionParam sectionParam) {
        Message<EvaluationOpeningScheduleVO> message = evaluationClient.openingSchedule(sectionParam);
        return ResultUtil.msg(message);
    }

}
